package cn.hairui.fastms.core.controller;


import cn.hairui.fastms.base.cache.CacheManage;
import cn.hairui.fastms.base.config.WebLogInfo;
import cn.hairui.fastms.constant.FieldConstant;
import cn.hairui.fastms.constant.GlobalConstant;
import cn.hairui.fastms.core.annotation.TableResult;
import cn.hairui.fastms.core.bean.SBch;
import cn.hairui.fastms.core.bean.SUser;
import cn.hairui.fastms.core.service.SBchService;
import cn.hairui.fastms.util.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 机构信息 前端控制器
 * </p>
 *
 * @author lihairui
 * @since 2020-12-26
 */
@Controller
@RequestMapping("/sBch")
public class SBchController {
    @Autowired
    private SBchService sBchService;

    @WebLogInfo(description = "机构信息列表页面")
    @RequestMapping(value = "/list")
    public String list(HttpServletRequest request) {
        return "pages/core/sbch/sbch";
    }

    @WebLogInfo(description = "机构信息新增页面")
    @RequestMapping(value = "/add")
    public String add(HttpServletRequest request) {
        return "pages/core/sbch/sbchAdd";
    }

    @WebLogInfo(description = "机构信息编辑页面")
    @RequestMapping(value = "/edit")
    public String edit(HttpServletRequest request) {
        return "pages/core/sbch/sbchEdit";
    }

    @WebLogInfo(description = "机构信息列表数据获取")
    @RequestMapping(value = "/data")
    @ResponseBody
    public TableResult data(HttpServletRequest request) {
        String pageSizeStr = request.getParameter("rows");
        String pageNumStr = request.getParameter("page");
        Integer pageNum = null;
        int pageSize = GlobalConstant.DEFAULT_PAGE_SIZE;
        if (pageNumStr != null) {
            pageNum = Integer.parseInt(pageNumStr);
        }
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSizeStr != null) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        PageHelper.startPage(pageNum, pageSize);

        List<SBch> sDicList = sBchService.list();
        PageInfo<SBch> pageInfo = new PageInfo<>(sDicList);
        return new TableResult(pageInfo.getTotal(), sDicList);
    }

    @WebLogInfo(description = "机构信息列表数据获取全部加载")
    @RequestMapping(value = "/dataS")
    @ResponseBody
    public String dataS(HttpServletRequest request, String status) {
        QueryWrapper<SBch> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(FieldConstant.BCH_STS[0], status);

        List<SBch> sBchList = sBchService.list(queryWrapper);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("rows", sBchList);
        return jsonObject.toString();
    }

    @WebLogInfo(description = "机构信息详细数据获取")
    @RequestMapping(value = "/detail")
    @ResponseBody
    public JSONObject data(HttpServletRequest request, String bchCde) {
        QueryWrapper<SBch> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(FieldConstant.BCH_CDE[0], bchCde);
        SBch sBch = sBchService.getOne(queryWrapper);
        JSONObject json = JSONObject.fromObject(sBch);
        return json;
    }

    @WebLogInfo(description = "机构信息数据保存")
    @RequestMapping(value = "/save")
    @ResponseBody
    public String save(HttpServletRequest request, SBch sBch, String flag) {
        SUser currentUser = (SUser) request.getSession().getAttribute(GlobalConstant.GLOBAL_SESSION_NAME);
        if (GlobalConstant.OPERATION_ADD.equals(flag)) {
            //新增保存
            //判断是否已经存在
            QueryWrapper<SBch> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(FieldConstant.BCH_CDE[0], sBch.getBchCde());

            if (sBchService.list(queryWrapper).size() > 0) {
                //已存在机构 不允许新增
                return CacheManage.msgMap.get(GlobalConstant.MSG_CDE_S0003).toJSONString();
            } else {
                //不存在机构 允许新增
                sBch.setCreateTime(DateUtil.getCurrentDateTimeStr());
                sBch.setCreateUser(currentUser.getUserAccount());
                sBch.setUpdateTime(DateUtil.getCurrentDateTimeStr());
                sBch.setUpdateUser(currentUser.getUserAccount());
                sBchService.save(sBch);
                return CacheManage.msgMap.get(GlobalConstant.MSG_CDE_S0000).toJSONString();
            }

        } else if (GlobalConstant.OPERATION_EDIT.equals(flag)) {
            //更新保存
            sBch.setUpdateTime(DateUtil.getCurrentDateTimeStr());
            sBch.setUpdateUser(currentUser.getUserAccount());
            boolean result = sBchService.saveOrUpdate(sBch);
            if (result) {
                //更新成功
                return CacheManage.msgMap.get(GlobalConstant.MSG_CDE_S0000).toJSONString();
            } else {
                //更新失败
                return CacheManage.msgMap.get(GlobalConstant.MSG_CDE_S0001).toJSONString();
            }
        } else {
            return null;
        }
    }

    @WebLogInfo(description = "机构信息数据删除")
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Transactional
    public String delete(HttpServletRequest request, String[] bchCde) {
        QueryWrapper<SBch> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(FieldConstant.BCH_CDE[0], bchCde);
        sBchService.remove(queryWrapper);
        return CacheManage.msgMap.get(GlobalConstant.MSG_CDE_S0000).toJSONString();
    }
}

